package com.edu.controller;

import com.edu.dao.UserDao;
import com.edu.data.Pager;
import com.edu.data.Product;
import com.edu.data.ProductCategory;
import com.edu.data.User;
import com.edu.util.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/User")
public class UserController {

    @Autowired
    UserDao userDao;

    @RequestMapping("/selectId")
    public String indexDetail(Model model, HttpServletRequest req) {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        model.addAttribute("user",user);
        return "../../manage/user";
    }

    @RequestMapping("/read")
    public String read(String id,Model model) throws SQLException {
        User user = userDao.findById(id);
//        String[] addr=user.getEuAddress().split(";");
//        model.addAttribute("addr", addr);
        model.addAttribute("user",user);
        return "../../manage/user-modify";
    }

    @RequestMapping("/save")
    private String save(HttpServletRequest req, User user,Model model) throws SQLException {
        Map<String,String> errors=new HashMap<String,String>();
        System.out.println(user);
        if(!user.getEuPassword().equals(user.getEuPassword())){
            errors.put("error_pass","两次输入的密码不一样");
        }
        if(user.getEuIdentityCode().length()!=18){
            errors.put("error_identityCode","身份证格式不一样");
        }
        if(!user.getEuEmail().endsWith(".com")){
            errors.put("error_email","邮箱格式不一样");
        }
        if(errors.size()>0){
            req.setAttribute("errorsuser",user);
            req.setAttribute("errors",errors);
            return "../../manage/user-modify";
        }else{
            req.getSession().setAttribute("loginUser", user);
            if(user.getEuUserName().equals("管理员")){
                user.setEuStatus(2);
                user.setEuLogin(Float.parseFloat("1"));
                model.addAttribute("loginUser", user);
                userDao.update(user);
                return "redirect:list";
            }else{
                user.setEuStatus(1);
                user.setEuLogin(Float.parseFloat("0"));
                model.addAttribute("loginUser", user);
                userDao.update(user);
                return "redirect:selectId";
            }
        }
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest request, Validator validator,Model model) {
//        String page = request.getParameter("page");
//        int pageNo = 1;
//        if (!Validator.isEmpty(page))
//            pageNo = Integer.parseInt(page);
//        int rowCount = userDao.getUserRowCount();
//        Pager pager = new Pager(rowCount, pageNo);
//        List<User> users = userService.getUsers(pager);
//        request.setAttribute("users", users);
//        request.setAttribute("pager", pager);
//        request.setAttribute("pageNo", pageNo);
        List<User> users = userDao.getUsers();
        model.addAttribute("users",users);
        return "../../manage/user";
    }

    @RequestMapping("/delete")
    public String delete(Model model,String id) throws SQLException {
        userDao.delete(id);
        return "redirect:list";
    }
}
